"""KIEL data format
Copyright (c) 2009 Christian T. Steigies <christian@steigies.net>

This module offers functions to read NM data in ASCII format as used in KIEL
and return data in the format as used by NMDB."""

__author__ = "Christian T. Steigies <christian@steigies.net>"
__license__ = "GPL License"

import os
# monthrange, isleap
import calendar

# http://www.scipy.org/Tentative_NumPy_Tutorial#head-926a6c9b68b752eed8a330636c41829e6358b1d3
from numpy import * # python-numpy

from nmdb.tools.datetool import YY

__all__ = ["read_1m", "read_1h"]

#DEBUG=True
DEBUG=False
## --------------------------------------------------------------------------
"""KIEL data availability
1h data since:
1957-07-01 C and O data only, no U or P data
1964-09-01 C, O, U and P data

1min data since:
1971-02-01 C, O and P data
"""
## --------------------------------------------------------------------------

def read_1m(datapath,station,year,month):
    """read in one month of minutely data from ASCII files
    and return data in NMDB format
    TODO: if DOS uppercase of subdirectories after datapath"""

    # use KIEL data for the LEIK test table
    if station =="leik":
        station = "kiel"

    fileC = "%s/ftp_nm1/c/%4ic/%4i%s/%02i%02i%s.w1c" \
            % (datapath, year, year, station, YY(year), month, station)
    fileP = "%s/ftp_nm1/p/%4ip/%4i%s/%02i%02i%s.w1p" \
            % (datapath, year, year, station, YY(year), month, station)
    fileU = "%s/ftp_nm1/u/%4iu/%4i%s/%02i%02i%s.w1u" \
            % (datapath, year, year, station, YY(year), month, station)

    try:
        myfileC = open(fileC, "r")
        myfileP = open(fileP, "r")
        myfileU = open(fileU, "r")
    except IOError:
        # any of the three files could not exist
        print "error opening %s" % (fileC)
        print "No data to be sent to NMDB!"
        sys.exit(0)
            
    myfileC = open(fileC, "r")
    myfileP = open(fileP, "r")
    myfileU = open(fileU, "r")
        
    (dummy, days) = calendar.monthrange(year,month)
    dataC = zeros((32,24,60), int)
    dataU = zeros((32,24,60), int)
    dataP = zeros((32,24,60), int)
        
    for DD in range(1,days+1):        
        for HH in range(0,24):
            C = U = P = []
                
            for i in range(0,5):
                line = myfileC.readline()
                # basic checks if data is correct: start
                if (i < 4) and (len(line.split()) > 12):
                    print "error in corrected data"
                elif (i == 4):
                    this_my = line.split()[12:13][0]
                    if (len(this_my) == 4):
                        this_year = int(this_my[0:2])
                        this_month = int(this_my[2:4])
                    else:
                        pass
                    this_day = int(line.split()[13:14][0])
                    this_hour = int(float(line.split()[14:15][0]))

                    if (this_year != YY(year)):
                        print "wrong year", YY(year), this_year, int(this_year)
                    if (this_month != month):
                        print "wrong month", month, this_month, int(this_month)
                    if (this_day != DD):
                        print "wrong day", DD, this_day, int(this_day)
                    if (this_hour != HH):
                        print "wrong hour", HH,  line.split()[14:15]
                # basic checks if data is correct: stop
                C = C + line.split()
                        
                line = myfileU.readline()
                # basic checks if data is correct: start
                if (i < 4) and (len(line.split()) > 12):
                    print "error in uncorrected data"
                # basic checks if data is correct: stop
                U = U + line.split()
                                    
                line = myfileP.readline()
                # basic checks if data is correct: start
                if (i < 4) and (len(line.split()) > 12):
                    print "error in pressure data"
                # basic checks if data is correct: stop
                P = P + line.split()
                        
            if (len(C) > 60) and (len(U) > 60) and (len(P) > 60):
                for i in range(0,60):
                    dataC[DD][HH][i] = C[i]
                    dataU[DD][HH][i] = U[i]
                    dataP[DD][HH][i] = P[i]
                    if DEBUG:
                        print "%02i-%02i-%04i %02i:%02i %6i %6i %6i" \
                        %(DD, month, year, HH, i, dataC[DD][HH][i], dataU[DD][HH][i], dataP[DD][HH][i])
            else:
                print "error in data files!"
                exit(1)
                                                
    myfileC.close()
    myfileP.close()
    myfileU.close()

    # convert data to NMDB requirements
    #corrected_for_pressure = dataC/60. # corrected for pressure [counts/sec]
    #corrected_for_efficiency = corrected_for_pressure     # corrected for efficiency [counts/sec]
    #uncorrected = dataU/60. # uncorrected [counts/sec]
    #pressure = dataP/10. # atmospheric pressure [mbar]

    #return(corrected_for_pressure,corrected_for_efficiency,uncorrected,pressure)
    return(dataC, dataU, dataP)

## --------------------------------------------------------------------------

def read_1h(datapath,station,year,month):
    """read in one month of hourly data from ASCII files
    and return data in NMDB format
    TODO: if DOS uppercase of subdirectories after datapath
    TODO: fail if error in data checks"""

    if station =="leik":
        station = "kiel"
        
    fileC = "%s/ftp_nm/c/%4ic/%4i%s/%02i%02i%s.c0c" \
            % (datapath, year, year, station, YY(year), month, station)
    fileP = "%s/ftp_nm/p/%4ip/%4i%s/%02i%02i%s.c0p" \
            % (datapath, year, year, station, YY(year), month, station)
    fileU = "%s/ftp_nm/u/%4iu/%4i%s/%02i%02i%s.c0u" \
            % (datapath, year, year, station, YY(year), month, station)
    
    useU = useP = True
    try:
        myfileC = open(fileC, "r")
    except IOError:
        print "error opening %s" % (fileC)
        sys.exit(0)
        
    try:
        myfileP = open(fileP, "r")
    except IOError:
        print "error opening %s" % (fileP)
        useP = False
        
    try:
        myfileU = open(fileU, "r")
    except IOError:
        print "error opening %s" % (fileU)
        useU = False
            
    (dummy, days) = calendar.monthrange(year,month)
    dataC = zeros((32,24), int)
    dataU = zeros((32,24), int)
    dataP = zeros((32,24), int)

    # skip comment lines
    for i in range(0,7):
        line = myfileC.readline()
        if useP:
            line = myfileP.readline()
        if useU:
            line = myfileU.readline()

    for DD in range(1,days+1):        
        for HH in range(0,24):               
            C = O = U = P = []
        for i in (1,2):
            line = myfileC.readline()
            C = C + line.split()
            if useP:
                line = myfileP.readline()
                P = P + line.split()
            if useU:
                line = myfileU.readline()
                U = U + line.split()

        if (len(C) > 24):
            for i in range(0,24):
                dataC[DD][i] = C[i]
                if useU:
                    dataU[DD][i] = U[i]
                if useP:
                    dataP[DD][i] = P[i]
                if DEBUG:
                    print "%02i-%02i-%04i %02i:%02i %6i %6i %6i" \
                          %(DD, month, year, i, 0, dataC[DD][i], dataU[DD][i], dataP[DD][i])

        else:
            print "error in data files!"
            exit(1)
                
    myfileC.close()
    if useP:
        myfileP.close()
    if useU:
        myfileU.close()

    # convert data to NMDB requirements
    #corrected_for_pressure = dataC*100./3600. # corrected for pressure [counts/sec]
    #corrected_for_efficiency = corrected_for_pressure     # corrected for efficiency [counts/sec]
    #uncorrected = dataU*100/3600. # uncorrected [counts/sec]
    #pressure = dataP/10. # atmospheric pressure [mbar]
    
    #return(corrected_for_pressure,corrected_for_efficiency,uncorrected,pressure)
    
    return(dataC, dataU, dataP)

## --------------------------------------------------------------------------

if __name__=="__main__":
    print "This is the KIEL_ASCII module for NMDB."

    datapath = "/home/kermes/steigies/.dosemu/drives/c/Inetpub/ftproot/CosRay!"
    station = "kiel"
    year = 2012
    month = 10
    (corrected_for_pressure,corrected_for_efficiency,uncorrected,pressure) = read_1m(datapath,station,year,month)
    #(corrected_for_pressure,corrected_for_efficiency,uncorrected,pressure) = read_1h(datapath,station,year,month)

